package top.kaoshanji.p1ch02c;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

/**
 * author kaoshanji
 * ^_^
 * create time 2025/1/2 19:29
 */
public class MethodHandleExample04 {

    private static final Logger logger = LogManager.getLogger(MethodHandleExample04.class);

    public static void main(String[] args) throws Throwable {

        MethodHandleExample04 example = new MethodHandleExample04();
        example.asFixedArity();

    }

    public void asFixedArity() throws Throwable {
        logger.info("...代码清单 p1ch02c-10：MethodHandle#asFixedArity的使用示例...");

        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle mh = lookup.findVirtual(MethodHandleExample04.class, "varargsMethod",
                MethodType.methodType(void.class, String.class, int[].class));

        mh = mh.asFixedArity();
        mh.invoke(this, "Hello", new int[]{2, 4});

        System.out.println(mh.type());

    }

    public void varargsMethod(String arg1, int... args) {}


}
